package com.sugon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleSpDemandItemDao;
import com.sugon.dao.ModuleSpDeptStockDao;
import com.sugon.dao.ModuleSpPurchasingBatchDao;
import com.sugon.entity.ModuleSpDemandItemEntity;
import com.sugon.entity.ModuleSpDeptStockEntity;
import com.sugon.entity.ModuleSpPurchasingBatchEntity;
import com.sugon.entity.ModuleSpStockEntity;
import com.sugon.service.ModuleSpDemandItemService;
import com.sugon.service.ModuleSpStockService;
import com.sugon.utils.ErmsUtil;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 模具型芯需求明细Service实现类
 *
 * @author YuXD
 * @date 2022-07-11
 */
@Service("moduleSpDemandItemService")
public class ModuleSpDemandItemServiceImpl implements ModuleSpDemandItemService {

    @Resource
    private ModuleSpDemandItemDao moldSpDemandItemDao;
    @Resource
    private ModuleSpDeptStockDao moldSpDeptStockDao;
    @Resource
    private ModuleSpPurchasingBatchDao moldSpPurchasingBatchDao;
    @Resource
    private ModuleSpStockService moldSpStockService;

    @Override
    public void realtimeUpdateNum(String id) {
        ModuleSpDemandItemEntity demandItem = queryObject(id);
        // 老数据是没有需求计划明细的，所以不做任何处理
        if (demandItem == null) {
            return;
        }
        List<ModuleSpStockEntity> moldSpStockList = moldSpStockService.queryListByDemandItemId(id);
        if (CollectionUtil.isNotEmpty(moldSpStockList)) {
            int stockInNum = moldSpStockList.size();
            /*
                由于入库的型芯有赔付类型的
                这种入库的只算作入库数，不算作在途数
             */
            int realStockInNum = (int) moldSpStockList.stream().filter(x -> !x.getUniqueCode().endsWith("P")).count();
            demandItem.setStockInNum(stockInNum);
            demandItem.setTransitNum(demandItem.getDemandNum() - realStockInNum);
            update(demandItem);
        }
    }

    @Override
    public ModuleSpDemandItemEntity query(String purchasingBatchId, String uniqueCode) {
        ModuleSpDemandItemEntity param = new ModuleSpDemandItemEntity();
        param.setPurchasingBatchId(purchasingBatchId);
        param.setWorkshop(ShiroUtils.getWorkshop());
        param.setDemandMonth(ErmsUtil.parseMonth(uniqueCode));
        List<ModuleSpDemandItemEntity> demandItemList = queryList(BeanUtil.beanToMap(param));
        return CollectionUtil.isNotEmpty(demandItemList) ? demandItemList.get(0) : null;
    }

    @Override
    public boolean batchAddDemandItem(String[] ids) {
        // 如果列表为空，则直接返回true，什么都不做
        List<ModuleSpDeptStockEntity> stockList = moldSpDeptStockDao.queryObjectList(ids);
        if (CollectionUtil.isEmpty(stockList)) {
            return true;
        }
        // 需求月份
        String demandMonth = com.sugon.modules.utils.DateUtil.nextMonth();
        for (ModuleSpDeptStockEntity stock : stockList) {
            // 1、首先校验是否已添加过，已添加的不再添加
            List<ModuleSpDemandItemEntity> demandItemList = moldSpDemandItemDao.query(stock.getId(), demandMonth);
            if (CollectionUtil.isNotEmpty(demandItemList)) {
                continue;
            }
            // 2、找出对应的型芯采购批次，如果没有，则继续下次
            List<ModuleSpPurchasingBatchEntity> purchasingBatchList = moldSpPurchasingBatchDao.findMatchedPurchasingBatch(stock.getMoldSpId(), demandMonth);
            if (CollectionUtil.isEmpty(purchasingBatchList)) {
                continue;
            }
            // 3、构建模具型芯需求明细
            ModuleSpDemandItemEntity demandItem = new ModuleSpDemandItemEntity();
            demandItem.setMoldSpId(stock.getMoldSpId());
            demandItem.setWorkshop(stock.getWorkshop());
            demandItem.setSupplier(purchasingBatchList.get(0).getSupplierId());
            demandItem.setUnitPrice(purchasingBatchList.get(0).getUnitPrice());
            demandItem.setPurchasingBatchId(purchasingBatchList.get(0).getId());
            demandItem.setDemandMonth(demandMonth);
            demandItem.setStockId(stock.getId());
            save(demandItem);
        }
        return true;
    }

    @Override
    public ModuleSpDemandItemEntity queryObject(String id) {
        return moldSpDemandItemDao.queryObject(id);
    }

    @Override
    public List<ModuleSpDemandItemEntity> queryList(Map<String, Object> map) {
        return moldSpDemandItemDao.queryList(map);
    }

    @Override
    public List<ModuleSpDemandItemEntity> queryByMoldSpId(String moldSpId) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("moldSpId", moldSpId);
        return queryList(param);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return moldSpDemandItemDao.queryTotal(map);
    }

    @Override
    public int save(ModuleSpDemandItemEntity moduleSpDemandItem) {
        moduleSpDemandItem.setId(RandomUtil.randomString(32));
        moduleSpDemandItem.setGmtCreate(DateUtil.date());
        moduleSpDemandItem.setCreateUser(ShiroUtils.getUserId());
        moduleSpDemandItem.setTransitNum(moduleSpDemandItem.getDemandNum());
        moduleSpDemandItem.setStockInNum(0);
        return moldSpDemandItemDao.save(moduleSpDemandItem);
    }

    @Override
    public int update(ModuleSpDemandItemEntity moduleSpDemandItem) {
        moduleSpDemandItem.setGmtModified(DateUtil.date());
        moduleSpDemandItem.setModifyUser(ShiroUtils.getUserId());
        return moldSpDemandItemDao.update(moduleSpDemandItem);
    }

    @Override
    public int delete(String id) {
        return moldSpDemandItemDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moldSpDemandItemDao.deleteBatch(ids);
    }
}
